package com.zzlzd.drp.util;

import cn.binarywang.wx.miniapp.api.WxMaService;
import com.zzlzd.drp.config.WxMA.WxMaConfiguration;
import me.chanjar.weixin.common.error.WxErrorException;
import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @author ceshi
 * @Title:
 * @Package
 * @Description:
 * @date 2021-5-2621:51
 */
public class WxTempMsg {
    private static String appId = "wx2f72c2fd30a9f48e";
    private static String secret = "380198d6f8fff0d0d42a3e08d233c75b";
    /***
     * 发送模板消息
     * @param openid
     * @param page
     */
    public static void sendMsg(String openid, String page, JSONObject templateDataList,String tempId) {
        final WxMaService wxService = WxMaConfiguration.getMaService(appId);

        StringBuilder url = new StringBuilder("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=");
        try {
            url.append(wxService.getAccessToken());//设置accesstoken
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
        JSONObject json = new JSONObject();
        json.put("touser", openid);//设置openid
        json.put("template_id", tempId);//设置模板id
        json.put("data", templateDataList);//设置模板消息内容
        json.put("page", page);//跳转微信小程序页面路径（非必须）
        try {
            HttpClient client = HttpClientBuilder.create().build();//构建一个Client
            HttpPost post = new HttpPost(url.toString());//构建一个POST请求
            StringEntity s = new StringEntity(json.toString(), "UTF-8");
            s.setContentEncoding("UTF-8");
            s.setContentType("application/json; charset=UTF-8");
            post.setEntity(s);//设置编码，不然模板内容会乱码
            HttpResponse response = client.execute(post);//提交POST请求
            HttpEntity result = response.getEntity();//拿到返回的HttpResponse的"实体"
            String content = EntityUtils.toString(result);
            System.out.println(content);//打印返回的消息
            JSONObject res = JSONObject.fromObject(content);//转为json格式
            System.out.println(res);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }




    public static void sendGift(String openid, String amount,String shop,String name) {
        final WxMaService wxService = WxMaConfiguration.getMaService(appId);

        StringBuilder url = new StringBuilder("https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=");
        try {
            url.append(wxService.getAccessToken());//设置accesstoken
        } catch (WxErrorException e) {
            e.printStackTrace();
        }


        JSONObject data = new JSONObject();
        JSONObject name1 = new JSONObject();
        name1.put("value","购买返佣");
        data.put("thing1",name1);

        JSONObject time4 = new JSONObject();
        time4.put("value",amount);
        data.put("amount2",time4);


        JSONObject thing3 = new JSONObject();
        thing3.put("value",shop);
        data.put("thing4",thing3);



        JSONObject date4 = new JSONObject();
        date4.put("value",name);
        data.put("thing7",date4);




        JSONObject json = new JSONObject();
        json.put("touser", openid);//设置openid
        json.put("template_id", "vBuenJw_P_hO7c4XL8RrLmF0NTmEYijORgi3frGxV2M");//设置模板id
        json.put("data", data);//设置模板消息内容
        json.put("page", "pages/me/gift/gift");//跳转微信小程序页面路径（非必须）
        try {
            HttpClient client = HttpClientBuilder.create().build();//构建一个Client
            HttpPost post = new HttpPost(url.toString());//构建一个POST请求
            StringEntity s = new StringEntity(json.toString(), "UTF-8");
            s.setContentEncoding("UTF-8");
            s.setContentType("application/json; charset=UTF-8");
            post.setEntity(s);//设置编码，不然模板内容会乱码
            HttpResponse response = client.execute(post);//提交POST请求
            HttpEntity result = response.getEntity();//拿到返回的HttpResponse的"实体"
            String content = EntityUtils.toString(result);
            System.out.println(content);//打印返回的消息
            JSONObject res = JSONObject.fromObject(content);//转为json格式
            System.out.println(res);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
